
#include <iostream>
#include <fstream>
#include <QGLWidget>
#include <QTimer>
#include <QDebug>

#include "DisplayWidget.h"
#include "SpaceExpWidget.h"

using namespace std;

DisplayWidget::DisplayWidget(QWidget *parent, SpaceExpWidget *spaceExpWidget) : QGLWidget(parent) {

    displayDevice = spaceExpWidget;

    qDebug()<< "DisplayWidget:DisplayWidget():Start Widget"<<endl;


    qDebug()<< "DisplayWidget:DisplayWidget():end Widget"<<endl;

}


DisplayWidget::~DisplayWidget() {

}



void DisplayWidget::initializeGL() {

    glClearColor(0.0f, 0.0f, 0.0f, 0.f);
    //resizeGL( 400 , 300 );

    glShadeModel( GL_SMOOTH );
    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
    glEnable( GL_TEXTURE_2D );
    glEnable( GL_CULL_FACE );
    glEnable( GL_DEPTH_TEST );

}


void DisplayWidget::paintGL() {
    //draw scene here

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  // Clear the screen and the depth buffer
    glLoadIdentity();  // Reset the view

    glTranslated(0.0, 0.0, -5.0);
    
    glBegin(GL_LINES); // Draw the direction arrow
        glVertex3f(0.0,0.0,0.0);
        glVertex3f(displayDevice->x,displayDevice->y,0.0);
    glEnd();
}


void DisplayWidget::updateImage() {

        updateGL();
        return;
}

void DisplayWidget::resizeGL(int width, int height)
{
    //proces resize keep good aspect ratio for 3D scene
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    int side = qMin(width, height);
    glViewport((width - side) / 2, (height - side) / 2, side, side);

   // glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (GLfloat)width/(GLfloat)height, 0.01f, 1000.0f);
    glMatrixMode(GL_MODELVIEW);
}



